package cn.thr.pethome.department.service.Impl;

import cn.thr.pethome.base.service.Impl.BaseServiceImpl;
import cn.thr.pethome.department.domain.Department;
import cn.thr.pethome.department.mapper.DepartmentMapper;
import cn.thr.pethome.department.query.DepartmentQuery;
import cn.thr.pethome.department.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void add(Department department) {
        buildDirPath(department);
        //修改当前的这条数据
        departmentMapper.update(department);
    }
    //构建dirpath 的路径
    private void buildDirPath(Department department){
        if (department.getParent_id()==null){ //没有父级直接传
            departmentMapper.add(department);
            //获得id
            Long id = department.getId();
            department.setDirPath("/"+id);
            //修改当前这条数据
            departmentMapper.update(department);
        }else { //有父级
            Long parentId = department.getParent_id();
            Department parent = departmentMapper.findOne(parentId); //查到父级所有数据
            String dirPath = parent.getDirPath();

            departmentMapper.add(department);
            department.setDirPath(dirPath+"/"+department.getId());
            //修改当前这条数据
            departmentMapper.update(department);
        }
    }

    @Override
    @Transactional
    public void update(Department department) {
        if(department.getId().equals(department.getParent_id())){
            throw  new RuntimeException("不能设置自己为自己的上级");
        }
        buildDirPath(department);
        departmentMapper.update(department);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        //业务逻辑，如果有就可以删除，没有就不能删除
        //这个使用来查询Parent 有没有id
        Long parentId = departmentMapper.findByParentId(id);
        //如果查询出来 parentId 不等于0 就可以进行删除，等于0或者没有的话就不行，没有是最高级
        if (parentId == 0){
            departmentMapper.delete(id);
        }else {
            throw  new RuntimeException("当前部门存在下级不能删除");
        }
    }
    //批量删除  不要写业务逻辑太麻烦了！ 暂时不写
   

    @Override
    public List<Department> deptTree() {
        //第一步查询全部
        List<Department> departments = departmentMapper.findAll();

        //第三步 儿子找父亲 建立好关系网
        for (Department department : departments) {
            if(department.getParent_id() != null){//找顶级 如过id不等于null就循环找儿子
                for (Department parent : departments) {
                    if(department.getParent_id() == parent.getId()){//判断是不是你的父亲
                        List<Department> children = parent.getChildren();
                        children.add(department);
                        break; //找到取消循环
                    }
                }
            }
        }
        //第二步找顶级
        List<Department> firstTree = departments.stream()
                .filter(department -> department.getParent_id() == null)//过滤出来父id为null 的话就过滤出来
                .collect(Collectors.toList());//转为list集合
        System.out.println(firstTree);
        return firstTree;
    }
}
